
# 快速创建插件的函数库

class_name myPlugs

static func createDockPlugs(plgName:String,dsc:String,author:String,version:String) -> void:
	var cfgFName = "plugin.cfg" # 插件元信息文件名
	var dir = Directory.new()
	# 创建addons文件夹
	var addons_path = "res://addons/"
	if not dir.dir_exists(addons_path):
		dir.make_dir(addons_path)
	# 创建addons/[plgName]文件夹
	var plug_path = addons_path  + plgName + "/"
	if not dir.dir_exists(plug_path):
		dir.make_dir(plug_path)
	# 创建"plugin.cfg"文件
	var file = File.new()
	var fPath = plug_path + cfgFName
	var fCont = "[plugin]\n\n"
	fCont += 'name="%s"\n' % plgName
	fCont += 'description="%s"\n' % dsc
	fCont += 'author="%s"\n' % author
	fCont += 'version="%s"\n' % version
	fCont += 'script="%s"\n' % (plgName + ".gd")
	if not file.file_exists(fPath):
		file.open(fPath,File.WRITE)
		file.store_string(fCont)
		file.close()
	# 创建并打开.tscn场景文件
	var tscnPath = plug_path + plgName + "UI.tscn"
	if not file.file_exists(tscnPath):
		file.open(tscnPath,File.WRITE)
		var tscnCont = '[gd_scene format=2]' + "\n".repeat(2)
		tscnCont += '[node name="Control" type="Control"]' + "\n"
		tscnCont += 'anchor_right = 1.0' + "\n"
		tscnCont += 'anchor_bottom = 1.0' + "\n"
		tscnCont += '__meta__ = {' + "\n"
		tscnCont += '"_edit_use_anchors_": false' + "\n"
		tscnCont += '}'
		file.store_string(tscnCont)
		file.close()
	# 创建创建"[plgName].gd"文件
	var gdPath = plug_path + plgName + ".gd"
	if not file.file_exists(gdPath):
		file.open(gdPath,File.WRITE)
		var gdCtn = "tool" + "\n"
		gdCtn += "extends EditorPlugin" + "\n".repeat(2)
		gdCtn += "var ui = preload(\"%s\").instance()" % (plgName + "UI.tscn") + "\n".repeat(2)
		gdCtn += "func _enter_tree():" + "\n"
		gdCtn += "\t" + "add_control_to_dock(EditorPlugin.DOCK_SLOT_LEFT_BL,ui)" + "\n"
		gdCtn += "\t" + "pass" + "\n".repeat(3)
		gdCtn += "func _exit_tree():" + "\n"
		gdCtn += "\t" + "remove_control_from_docks(ui)" + "\n"
		gdCtn += "\t" + "ui.free()" + "\n"
		gdCtn += "\t" + "pass" + "\n"
		file.store_string(gdCtn)
		file.close()
	
	
